#!/usr/bin/env bash

# OSlist: https://git-scm.com/download/linux
# Linux
export OS_DEBIAN="Debian"
export OS_UBUNTU="Ubuntu"
export OS_REDHAT="RedHat" # Red Hat Enterprise Linux, Oracle Linux, CentOS, Scientific Linux, et al.
export OS_FEDORA="Fedora"
export OS_ARCH="ArchLinux"
export OS_GENTOO="Gentoo"
export OS_FUNTOO="Funtoo"
export OS_EXHERBO="Exherbo"
export OS_SUSE="openSUSE"
export OS_SLACKWARE="Slackware"
export OS_NIXOS="NixOS"
export OS_ALPINE="Alpine" # musl libc| busybox https://www.linuxprobe.com/alpine-linux-released.html
export OS_VOID="VoidLinux"
export OS_MAGEIA="Mageia" # Mandriva Linux
export OS_SLITAZ="Slitaz" # busybox
export OS_GUIXSD="GuixSD"
# Unix
export OS_MACOS="macOS"
export OS_AIX="AIX"
export OS_SOLARIS="Solaris" #OpenCSW illumos(kernel):OpenIndiana SmartOS DogeOS
export OS_FREEBSD="FreeBSD"
export OS_OPENBSD="OpenBSD"
export OS_NETBSD="NetBSD"
export OS_DFBSD="DragonflyBSD"
export OS_TRUEOS="TrueOS"
# Windows
export OS_CYGWIN="Cygwin"
export OS_MSYS2="MSYS2"
# Other
export OS_ANDROID="Android"
export OS_UNKNOW="Unknow"

# PKG-list: Yum APT SUSE AUR

# Arch-list:
#https://github.com/llvm-mirror/llvm/blob/a1cdcbb384fcf1a0332b3d8b7a9240e5c3ca5a14/docs/GettingStarted.rst#local-llvm-configuration
# AArch64, AMDGPU, ARM, BPF, CppBackend, Hexagon, Mips, MSP430, NVPTX, PowerPC, Sparc, SystemZ X86, XCore

export OME_ARCH="`uname -m`"
# export OME_SHELL="`ps | grep $$ | awk '{print $4}'`"
export OME_SHELL="`basename $SHELL`"

# case "`uname -o`" in
case "`uname -s`" in
  "GNU/Linux" | "Linux")
    if [ "$(uname -o)" = Android ]; then
      export OME_OS=$OS_ANDROID
    elif [ "`command -v zypper`" != "" ]; then
      export OME_OS=$OS_SUSE
      export releasever=`lsb-release -rs`
    elif [ "`command -v yum`" != "" ]; then
      os=`cat /etc/system-release | head -n 1 | cut -d ' ' -f 1`
      if [ "$os" = "CentOS" ] || [ "$os" = "RedHat" ]; then
        export OME_OS=$OS_REDHAT
      else
        export OME_OS=$OS_FEDORA
      fi
      # elif [ "`command -v yum`" != "" ]; then
    elif [ "`command -v guix`" != "" ]; then
      export OME_OS=$OS_GUIXSD
    elif [ "lsb_release" != "" ]; then
      case "`lsb_release -is`" in
        "Debian")
          export OME_OS=$OS_DEBIAN;;
        "Ubuntu"|"elementary"|"LinuxMint"|"Linuxmint")
          export OME_OS="$OS_UBUNTU";;
        "ManjaroLinux"|"Arch")
          export OME_OS=$OS_ARCH;;
        *)
          export OME_OS="`lsb_release -is`";;
      esac
    elif [ "`command -v ego`" != "" ]; then
      export OME_OS=$OS_FUNTOO
    elif [ "`command -v cave`" != "" ]; then
      export OME_OS=$OS_EXHERBO
    elif [ "`command -v emerge`" != "" ]; then
      export OME_OS=$OS_GENTOO
    elif [ "`command -v nix`" != "" ]; then
      export OME_OS=$OS_NIXOS
    else
      export OME_OS=$OS_UNKNOW
    fi
    ;;
  CYGWIN*) export OME_OS=$OS_CYGWIN;;
  MINGW* | MSYS*) export OME_OS=$OS_MSYS2;;
  FreeBSD) export OME_OS=$OS_FREEBSD;;
  DragonFly) export OME_OS=$OS_DFBSD;;
  NetBSD) export OME_OS=$OS_NETBSD;;
  Darwin) export OME_OS=$OS_MACOS;;
  *) export OME_OS=$OS_UNKNOW;;
esac

################################################################################

# Oh-My-Emacs
# export OME_BIN=$(dirname $(readlink -f $0))
# export OME_ROOT=$OME_BIN/..
export OME_ROOT=$HOME/.oh-my-env
export OME_BIN=$OME_ROOT/bin
export OME_PREFIX=$HOME/.ome_local
export OME_CACHE=$OME_PREFIX/caches
export OME_REPO=$OME_PREFIX/repo
export OME_DESTDIR=$OME_PREFIX/destdir

export OME_WS=$HOME/workspace
export OME_BOOK=$HOME/book
export OME_PROJ=$HOME/projects


# Emacs
#export ALTERNATE_EDITOR=""
export OME_EMACS=$HOME/.oh-my-emacs
export OME_EMACS_MODULE=$HOME/.oh-my-emacs/conf/modules
export OME_EMACS_SNIPPET=$HOME/.oh-my-emacs/conf/snippets
export TERM=xterm-256color

# lib
. $HOME/.oh-my-env/bin/lib
. $HOME/.oh-my-env/bin/tool
. $HOME/.oh-my-env/bin/proj

# dotnet
export PATH="$PATH:$HOME/.dotnet/tools"

# Clojure CLR
export CLJCLR14_40="$HOME/clojure-clr/Clojure/Clojure.Main461/bin/Debug/net461"

################################################################################
# User Settings
# if [ -d /mingw64/bin ]; then
# export PATH=/mingw64/bin:"$PATH"
# export MANPATH=/mingw64/share/man:$MANPATH
# export INFOPATH=/mingw64/share/info:$INFOPATH
# export C_INCLUDE_PATH=/mingw64/include:$C_INCLUDE_PATH
# export CPLUS_INCLUDE_PATH=/mingw64/include:$CPLUS_INCLUDE_PATH
# export LIBRARY_PATH=/mingw64/lib:$LIBRARY_PATH
# export LD_LIBRARY_PATH=/mingw64/lib:$LD_LIBRARY_PATH
# export PKG_CONFIG_PATH=/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig:$PKG_CONFIG_PATH
# fi
ome_add_path() {
  if [ -d "$1" ]; then
    local oldir=`pwd`
    cd $1
    local dir=`pwd`
    export PATH="$dir":"$PATH"
    local debug_mode="${OME_DEBUG_MODE:-unset}"
    if [ "${debug_mode}" == "1" ]; then
      echo_kv "ome_add_path" "$dir"
    fi
    # if [ "$OME_OS" = "$OS_CYGWIN" ] || [ "$OME_OS" = "$OS_MSYS2" ]; then
    # export PATH=`cygpath -m $dir`:"$PATH"
    # if [ "$OME_DEBUG_MODE" != "" ]; then
    # echo_kv "ome_add_path" "`cygpath -m $dir`"
    # fi
    # fi
    cd $oldir
  fi
}

ome_dev_path() {
  local dir="$1"
  if [ -d ${dir} ]; then
    local debug_mode="${OME_DEBUG_MODE:-unset}"
    if [ "${debug_mode}" == "1" ]; then
      echo_kv "ome_dev_path" "${dir}/bin"
    fi
    export PATH=$dir/bin:$PATH
    export MANPATH=$dir/share/man:${MANPATH:-""}
    export INFOPATH=$dir/share/info:${INFOPATH:-""}
    export LIBRARY_PATH=$dir/lib:${LIBRARY_PATH:-""}
    export LD_LIBRARY_PATH=$dir/lib:${LD_LIBRARY_PATH:-""}
    export LD_RUN_PATH=$dir/lib:${LD_RUN_PATH:-""}
    export PKG_CONFIG_PATH=$dir/lib/pkgconfig:$dir/share/pkgconfig:${PKG_CONFIG_PATH:-""}
    # libtool --finish $dir/lib

    export CMAKE_INCLUDE_PATH=$dir/include:${CMAKE_INCLUDE_PATH:-""}
    export CMAKE_LIBRARY_PATH=$dir/lib:${CMAKE_LIBRARY_PATH:-""}

    # export C_INCLUDE_PATH=$dir/include:${C_INCLUDE_PATH:-""}
    # export CPLUS_INCLUDE_PATH=$dir/include:${CPLUS_INCLUDE_PATH:-""}
  fi
}

# export PATH=$OME_WS/bin:$OME_BIN:"$PATH"

ome_add_path $OME_WS/bin
ome_add_path $OME_BIN

ome_dev_path $OME_PREFIX
ome_dev_path $HOME/.local

# if [ "$OME_OS" = "$OS_CYGWIN" ] || [ "$OME_OS" = "$OS_MSYS2" ]; then
# ome_dev_path `cygpath -m $OME_PREFIX`
# ome_dev_path `cygpath -m $HOME/.local`
# fi

# alias
if [ ! "$OME_OS" = "$OS_MACOS" ]; then
  alias ls='ls --color'
fi
alias ll='ls -l'
alias wget='wget -c'
alias omecd='f(){ cd $OME_ROOT/$1; ls; unset -f f; }; f'
alias pjcd='f(){ cd $OME_PROJ/$1; ls; unset -f f; }; f'
alias emcd='f(){ cd $OME_EMACS/$1; ls; unset -f f; }; f'
alias repocd='f(){ cd $OME_REPO/$1; ls; unset -f f; }; f'

if [ "$OME_OS" = "$OS_CYGWIN" ] || [ "$OME_OS" = "$OS_MSYS2" ]; then
  export LANG=en_US.UTF-8
fi

if [ "$OME_OS" = "$OS_MSYS2" ]; then
  export MSYS="winsymlinks:lnk"
fi

if [ "$OME_OS" = "$OS_DEBIAN" ] || [ "$OME_OS" = "$OS_UBUNTU" ]; then
  alias apt-dist-upgrade='f(){ sudo apt update && sudo apt dist-upgrade; unset -f f; }; f'
fi

# editor
export EDITOR=em

################################################################################
# FIX
#
# Fix for Ubuntu: emacs crash on Ubuntu
#if [ "$OME_OS" = "$OS_UBUNTU" ]; then
#  export XLIB_SKIP_ARGB_VISUALS=1
#fi

# FIX for macOS
if [ "$OME_OS" = "$OS_MACOS" ]; then
  # FIX: "readlink: illegal option -- f"   https://blog.csdn.net/libing_zeng/article/details/82953038
  export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
  alias readlink=greadlink

  # FIX: bashrc https://blog.csdn.net/liu510817387/article/details/8126719
  if [ ! -f $HOME/.bash_profile ]; then
    echo 'if [ "${BASH-no}" != "no" ]; then' > $HOME/.bash_profile
    echo '    [ -r ~/.bashrc ] && . ~/.bashrc' >> $HOME/.bash_profile
    echo 'fi' >> $HOME/.bash_profile
  fi

  if [ ! -f $HOME/.bashrc ]; then
    echo "# bashrc" > $HOME/.bashrc
  fi
fi

# FIX `xdg-open' for MSYS2
if [ "$OME_OS" = "$OS_MSYS2" ]; then
  if [ "`command -v xdg-open`" = "" ]; then
    echo '#!/usr/bin/env sh' > /usr/bin/xdg-open
    echo "" >> /usr/bin/xdg-open
    echo 'cygstart $@' >> /usr/bin/xdg-open
    chmod +x /usr/bin/xdg-open
  fi
  if [ "`command -v emacs`" = "" ]; then
	  export PATH=$PATH:/c/msys64/mingw64/bin:/c/msys64/ucrt64/bin
  fi
  if [ "`command -v emacs`" = "" ]; then
	  export PATH=$PATH:/d/msys64/mingw64/bin:/d/msys64/ucrt64/bin
  fi
  if [ "`command -v emacs`" = "" ]; then
	  export PATH=$PATH:/e/msys64/mingw64/bin:/e/msys64/ucrt64/bin
  fi
  if [ "`command -v emacs`" = "" ]; then
	  export PATH=$PATH:/f/msys64/mingw64/bin:/f/msys64/ucrt64/bin
  fi
  if [ "`command -v emacs`" = "" ]; then
	  export PATH=$PATH:/g/msys64/mingw64/bin:/g/msys64/ucrt64/bin
  fi
fi

# FIX `LLVM` path for windows
if [ "$OME_OS" = "$OS_CYGWIN" ] || [ "$OME_OS" = "$OS_MSYS2" ]; then
  export PATH="/c/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/x64/bin":"$PATH"
  export PATH="/c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin":"$PATH"
fi

# lang
export OME_MAKE="Ninja"
export MAKE_PROG="make"

# Language I
export ROSWELL_VERSION=23.10.14.114
export ROSWELL2_VERSION=23.9.20.0
if [ "$OME_OS" = "$OS_CYGWIN" ] || [ "$OME_OS" = "$OS_MSYS2" ]; then
  export SBCL_VERSION=2.4.1
else
  export SBCL_VERSION=2.4.1
fi

# Language II
export ERLANG_VERSION=26.2.2
export OCAML_VERSION=5.1.1
export STACK_VERSION=2.11.1
export GHC_VERSION=9.6.4
export PYTHON2_VERSION=2.7.18
export PYTHON3_VERSION=3.13.0
export RUBY_VERSION=3.3.0
# export PHP_VERSION=5.6.39
export PHP_VERSION=8.3.2

export JAVA_VERSION="18.ea.22-open"
# export JAVA_VERSION="21.3.0.r17-grl"
export JBANG_VERSION=0.114.0
export ANT_VERSION=1.10.14
export MAVEN_VERSION=3.9.6

# Language III
export GROOVY_VERSION=5.0.0
export GRADLE_VERSION=8.6.0
export GRAILS_VERSION=6.1.2
export KOTLIN_VERSION=1.9.22
export SCALA_VERSION=3.3.1
export SBT_VERSION=1.9.8
export CLOJURE_VERSION=1.12.0.1479
export LEIN_VERSION=2.11.1
export LEIN_JAR_URL="https://codeberg.org/attachments/43cebda5-a7c2-405b-b641-5143a00051b5"
export GOLANG_VERSION=1.22.0
export NODEJS_VERSION=20.11.0

# Language IV
export NIM_VERSION=2.0.2
export PONY_VERSION=git
export ZIG_VERSION=0.11.0

# Other
export ANTLR3_VERSION=3.5.3
export ANTLR4_VERSION=4.13.1
export ST_VERSION=4.3.4 #StringTemplate

export BC4_VERSION=4.4.7.28397
export JETBRINS_FONT_VERSION=2.304
export POWERSHELL_VERSION="7.4.1"
################################################################################
# load plugins
ome_load_pkgs() {
  local dir="$1"
  for pkg in $dir/* ; do
    if [ -d "$pkg" ]; then
      ome_load_pkgs $pkg
    else
      local debug_mode="${OME_DEBUG_MODE:-unset}"
      if [ "`basename $pkg`" != "README.org" ]; then
        if [ "${debug_mode}" == "1" ]; then
          # time "$pkg">/dev/null 2>&1
          tip "$pkg"
          # time `. $pkg`
        fi
        . "$pkg"
      fi
    fi
  done
}
ome_load_pkgs $HOME/.oh-my-env/packages
